<template>
  <div>
    <!-- 字典列表 -->
    <el-row :gutter="10">
      <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12" >
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>任务列表</span>
          </div>
          <!--工具栏-->
          <div class="head-container">
            <!-- 搜索 -->
            <el-input v-model="query.blurry" clearable size="small" placeholder="输入任务编码或者名称搜索" style="width: 200px;" class="filter-item" @keyup.enter.native="toQuery" />
            <el-button class="filter-item" size="mini" type="success" icon="el-icon-search" @click="toQuery">搜索</el-button>
            <el-button class="filter-item" size="mini" type="primary" icon="el-icon-plus" @click="addMutexTask">新增互斥</el-button>
          </div>
          <!--表格渲染-->
          <el-table v-loading="loading" :data="data" style="width: 100%;"
                    @selection-change="selectionLineChangeHandle" :header-cell-style="{ background: 'rgba(52, 125, 225, 0.1)', color: '#404248' }" border stripe >
            <el-table-column type="selection"/>
            <el-table-column prop="appCode" label="应用编号" />
            <el-table-column prop="taskCode" label="任务编码" />
            <el-table-column prop="taskName" label="任务名称" />
            <el-table-column prop="resNum" label="资源数" />
          </el-table>
          <!--分页组件-->
          <el-pagination
                  :total="total"
                  :current-page="page + 1"
                  style="margin-top: 8px;"
                  :pager-count="3" prev-text=" " next-text=" "
                  layout="prev, pager, next, sizes,total"
                  @size-change="sizeChange"
                  @current-change="pageChange"
          />
        </el-card>
      </el-col>
      <!-- 字典详情列表 -->
      <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>互斥任务列表</span>
          </div>
          <mutexTaskList ref="mutexTaskList" />
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import crud from '@/mixins/crud'
import mutexApi from '@/api/mutexPool/mutex'
import mutexTaskList from './mutexTaskList'

export default {
  mixins: [crud],
  props: {
    initTaskCodeListCallBack: {
      type: Function,
      default: null
    }
  },
  components: {mutexTaskList },
  data() {
    return {
      title: 'taskList',
      sort: ['id,desc'],
      groupCode: '',
      form: { id: null, label: null, value: null, defaultParam: null, dict: { id: null }, sort: 999 },
      rules: {
        label: [
          { required: true, message: '请输入字典标签', trigger: 'blur' }
        ],
        value: [
          { required: true, message: '请输入字典值', trigger: 'blur' }
        ],
        sort: [
          { required: true, message: '请输入序号', trigger: 'blur', type: 'number' }
        ]
      },
      totalElements: 0,
      dataonLineListSelections: [],
    }
  },
  created() {
    this.$nextTick(() => {
      this.init()
      this.$emit('initTaskCodeListCallBack');
    })
  },
  methods: {
    // 获取数据前设置好接口地址
    beforeInit() {
      this.url = 'api/periodTaskDef/selectPage'
      return true
    },
    initTaskCodeList(appCodeList, groupCode){
      this.groupCode = groupCode;
      this.query.appCodeList = appCodeList;
      this.toQuery();
      this.$refs.mutexTaskList.initMutexTaskList(groupCode);
    },
    selectionLineChangeHandle (val) {
      this.dataonLineListSelections = val
    },
    addMutexTask  () {
      if(this.dataonLineListSelections.length > 0 ){
        this.$confirm(`确认将选中的${this.dataonLineListSelections.length}条任务添加到互斥池吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let param = {taskList: this.dataonLineListSelections, groupCode: this.groupCode};
          mutexApi.addAdminMutexPool(param).then(()=>{
            this.$notify({  message: '添加成功', type: 'success'});
            this.$refs.mutexTaskList.initMutexTaskList(this.groupCode);
          }).catch((res) =>{
            this.$notify.error({title: '添加失败', message: res.response.data});
          })
        }).catch(() => {
        })
      }else{
        this.$notify({ title: '警告',  message: '请选择至少一条任务', type: 'warning'});
      }
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  ::v-deep .el-input-number .el-input__inner {
    text-align: left;
  }
</style>
